<?php
	class Category_News extends MY_Controller{	
		function Category_News() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();		
			$_SESSION['redirect'] = $this->uri->uri_string();
		}

		function index(){
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Danh mục tin tức";
			$data['table'] = 'category_news';
			$data['controller'] = 'category_news';
			$data['content_admin'] = 'back_end/category_news/index';
			$data['sort'] = 'ASC';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			$per_page = 20;
			$uri = 4;
			$data['admin_category_news'] = $this->mod_lib->load_all('category_news','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url('back_end/admin_menu/index/');
			$config['total_rows']   =  $this->db->count_all('category_news');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);	
		} 
		
		function sort($field,$sort = ''){
			$per_page = 20;
			$uri = 4;
			$data['title'] = "Category News - Sort";
			$data['table'] = 'category_news';
			$data['controller'] = 'category_news';
			$data['list_menu'] = $this->mod_lib->findOrderList($data['table']);
			$data['content_admin'] = 'back_end/category_news/index';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('category_news','',array(),array(),array("id"=>"desc"));
			$data['admin_category_news'] = $this->mod_lib->load_search('category_news','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
			$config['base_url']     =  site_url('back_end/category_news/index');

			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);		
		}
		
		function add(){
			$data['title']			= "Thêm danh mục tin tức";
			$data['content_admin'] = 'back_end/category_news/add';
			$data['list_menu'] = $this->mod_lib->findOrderListCategory('category_news');
			$data['table'] = 'category_news';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$add = $this->input->post('addform');
			
			if(!empty($add)){ 	
				$data['admin_menu_value'] = $this->db->get('admin_menu')->result_array();
				$data['content_admin'] = 'back_end/category_news/index';
					
				if( $this->input->post('parent_id') == 0) {
					$arr_pr  = array();
				} else {
					$this->db->select('root_path,id');
					$arr_menu = $this->db->get_where('category_news', array('id' => $this->input->post('parent_id')))->row_array();
					$arr_pr   = explode(".",$arr_menu['root_path']);
				}
				
				if(count($arr_pr) == 0) {
					$level = 1;
				} if (count($arr_pr) == 1){
					$level = 2;  	
				} else if (count($arr_pr) == 2) {
					$level = 3;
				}
				
				$data_value = array (
							'name'       		=> trim($this->input->post('name')),
							'parent_id'	=> trim($this->input->post('parent_id')),
							'alias'      		=> trim($this->input->post('alias')),
							'status'     		=> trim($this->input->post('status')),
							'level'      		=> $level				
						);
				
				if($this->mod_lib->insert('category_news',$data_value)) {
					$id = $this->db->insert_id();
				  	$where = array(
				  			'id' => $id 
				  		);
				  	$order = array (	
				  			'id' => 'desc'
				  		);	
			
				  	if ($this->input->post('parent_id') > 0) {
						$data  = array(
							'root_path'  => $arr_menu['root_path'].'.'.$id,
							'parent_id'  => $this->input->post('parent_id')	
						);
				  	} else {
				  		$data  = array(
				  			'root_path' => $id,
				  			'parent_id' => $this->input->post('parent_id')	
				  		);
				  	}
				
					$this->mod_lib->update('category_news',$where,$order,$data);
					$this->session->set_flashdata('msview', 'Thêm thành công !');
					$this->mod_lib->insert_log('Add admin_menu',$_SESSION['admin_id']);
				} else {	
					$this->session->set_flashdata('msview', 'Thêm thất bại !');
					$this->load->view('back_end/template_backend',$data);
				}
							
				redirect('back_end/category_news');
							
			} else {
				$this->load->view('back_end/template_backend',$data);
			}
		}
		
		function edit($id = null){
			$data['title']			= "Sửa danh mục tin tức";
			$data['content_admin'] = 'back_end/category_news/edit';
			$data['list_menu'] =  $this->mod_lib->findOrderListCategory('category_news');
			$data['admin_category_news_value'] = $this->db->get_where('category_news',array( 'id' => $id ))->row_array();
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			if(!empty($edit)){
				if ($id == $this->input->post('parent_id')) {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
					redirect('back_end/category_news');
				}
					
				if( $this->input->post('parent_id') == 0) {
					$arr_pr  = array();
				} else {
					$this->db->select('root_path,id');
					$arr_menu = $this->db->get_where('category_news', array('id' => $this->input->post('parent_id')))->row_array();
					$arr_pr  = explode(".",$arr_menu['root_path']);
				}
				
				if(count($arr_pr) == 0) {
					$level = 1;
				} if (count($arr_pr) == 1){
					$level = 2;  	
				} else if (count($arr_pr) == 2) {
					$level = 3;
				}
				
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC' 
			  		);
			  		
			  	if ($this->input->post('parent_id') > 0) {
					$data_value = array (
						'name'       => trim($this->input->post('name')),
						'status'     => trim($this->input->post('status')),
						'alias'     => trim($this->input->post('alias')),
						'level'      => $level,	
						'root_path'  => $arr_menu['root_path'].'.'.$id,
						'parent_id'  => $this->input->post('parent_id')
					);
			  	} else {
			  		$data_value = array (
						'name'       => trim($this->input->post('name')),
						'status'     => trim($this->input->post('status')),
			  			'alias'     => trim($this->input->post('alias')),
						'level'      => $level,	
						'parent_id'  =>  $this->input->post('parent_id'),
			  			'root_path' =>  $id
					);
			  	}
		
				if($this->mod_lib->update('category_news',$where,$order,$data_value)){
					$this->session->set_flashdata('msview', 'Sửa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
				}
				
				redirect('back_end/category_news');
			}else {		
				$this->load->view('back_end/template_backend',$data);		
			}
		}
		
		function deleteAll() {
			$value_id_arr    = $this->input->post('value_id');
		
			if(!empty($value_id_arr)){
				if($this->mod_lib->deleteCategory('category_news',$value_id_arr)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}
			
			redirect('back_end/category_news');
		}
		
		function delete($id) {
			if(!empty($id)){
				if($this->mod_lib->deleteCategory('category_news',$id)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			redirect('back_end/category_news');
		} 
		
	}
?>